﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Processing;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Processing
{
    /// <summary>
    /// 根据生产批次获取深加工信息请求命令处理类
    /// </summary>
    public class GetProcessingHandler:IRequestHandler<GetProcessingCommand,ApiResult<List<ProcessingDTO>>>
    {
        IBaseRepository<Processing_Information> processingRep;
        IMapper mapper;

        public GetProcessingHandler(IBaseRepository<Processing_Information> processingRep, IMapper mapper)
        {
            this.processingRep = processingRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<ProcessingDTO>>> Handle(GetProcessingCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<ProcessingDTO>> res = new ApiResult<List<ProcessingDTO>>();

                var processing = processingRep.GetAll().Where(x => x.Production_Batch == request.production_Batch && !x.IsDeleted);
                var model = mapper.Map<List<ProcessingDTO>>(processing);
                res.Msg = "根据生产批次获取深加工信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
